package com.tiantian.web.controller.auth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiantian.bean.PageBean;
import com.tiantian.domain.BackstageUser;
import com.tiantian.ret.Result;
import com.tiantian.service.BackstageRoleService;
import com.tiantian.service.BackstageUserService;
import com.tiantian.service.vo.UserVo;
import com.tiantian.util.PageUtil;
import com.tiantian.web.datatables.DataTableReturn;

@Controller
@RequestMapping("/auth/user")
public class BackstageUserController {
	public static final Logger LOG = LoggerFactory.getLogger(BackstageUserController.class);
	@Autowired
	private BackstageUserService backstageUserService;
	@Autowired
	private BackstageRoleService backstageRoleService;

	/**
	 * 后台管理用户列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/index" })
	public String index(Model model) {
		return "auth/user";
	}

	@ResponseBody
	@RequestMapping(value = { "/getUserList" })
	public DataTableReturn getUserList(HttpServletRequest request) {
		PageBean page = PageUtil.parseRequest2PageBean(request);
		Map<String, String> params = new HashMap<String, String>();
		List<BackstageUser> data = backstageUserService.list(params, page);
		return new DataTableReturn(page.getDraw(), page.getTotal(), page.getTotal(), data);
	}

	@ResponseBody
	@RequestMapping(value = { "/editstatus" })
	public Result<Object> editstatus(@RequestParam String userId, @RequestParam String status) {
		try {
			BackstageUser backstageUser = backstageUserService.get(userId);
			if (StringUtils.equals("1", status)) {
				backstageUser.setUserstatus("0");
				backstageUserService.save(backstageUser);
				return new Result<>(true, "禁用用户成功");
			} else {
				backstageUser.setUserstatus("1");
				backstageUserService.save(backstageUser);
				return new Result<>(true, "启用用户成功");
			}
		} catch (Exception e) {
			LOG.error("用户状态修改异常！", e);
			return new Result<>(false, "用户状态修改异常！" + e.getMessage());
		}
	}

	@RequestMapping(value = { "/userdetail" })
	public String userdetail(Model model, String id) {
		if (StringUtils.isNotBlank(id)) {
			BackstageUser backstageUser = backstageUserService.get(id);
			model.addAttribute("user", backstageUser);
		}
		model.addAttribute("roles", backstageRoleService.findAll());
		// 获取用户的权限列表
		List<Map<String, Object>> list = backstageUserService.findRoles(id);
		Map<String, Object> userRole = new HashMap<String, Object>();
		for (Map<String, Object> entry : list) {
			userRole.put((String) entry.get("id"), "on");
		}
		model.addAttribute("userRole", userRole);
		return "auth/userdetail";
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = { "/saveUser" })
	public Result<Object> saveUser(UserVo user, HttpServletRequest request) {
		try {
			Map<String, Object> params = request.getParameterMap();
			List<String> ids = new ArrayList<String>();
			for (Map.Entry<String, Object> entry : params.entrySet()) {
				if (entry.getKey().startsWith("check_")) {
					ids.add(entry.getKey().substring(6));
				}
			}
			return backstageUserService.saveUser(user, ids);
		} catch (Exception e) {
			LOG.error("保存用户异常！", e);
			return new Result<>(false, "用户状态修改异常！" + e.getMessage());
		}
	}
}
